package com.fastfood.controller;

import java.io.File;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;

import org.apache.struts2.ServletActionContext;

import com.fastfood.model.blo.FoodBLO;
import com.fastfood.model.blo.FoodTypeBLO;
import com.fastfood.model.dto.Food;
import com.fastfood.model.dto.FoodType;
import com.fastfood.util.CommonConstants;
import com.fastfood.util.FileUploadUtil;
import com.opensymphony.xwork2.ActionSupport;

public class FoodAction extends ActionSupport {

	private static final long serialVersionUID = 1L;

	private FoodBLO foodBLO;
	private FoodTypeBLO foodTypeBLO;
	
	private List<Food> listFood;
	private List<FoodType> listFoodType;
	private Food food;
	
	private File fileUpload;
	private String fileUploadContentType;
	private String fileUploadFileName;
	
	public FoodAction() {
		foodBLO = new FoodBLO();
		foodTypeBLO = new FoodTypeBLO();
		listFood = new ArrayList<Food>();
		listFoodType = new ArrayList<FoodType>();
	}

	public String list() {
		List<Food> listFood = foodBLO.getAllFoods();
		if (listFood != null) this.listFood = listFood;
		List<FoodType> listFoodType = foodTypeBLO.getAllFoodTypes();
		if (listFoodType != null) this.listFoodType = listFoodType;
		return SUCCESS;
	}
	
	public String add() {
		String destFile = CommonConstants.FOOD_IMAGE_DIR + CommonConstants.SEPARATOR + fileUploadFileName;
		Path path = Paths.get(ServletActionContext.getServletContext().getRealPath(destFile));
		
		if (FileUploadUtil.uploadFile(fileUpload, path) == false) {
			destFile = CommonConstants.FOOD_IMAGE_DIR + CommonConstants.SEPARATOR + CommonConstants.DEFAULT_IMAGE_FILE;
		}
		food.setImage(destFile);
		foodBLO.addFood(food);
		return SUCCESS;
	}
	
	public String update() {
		String destFile = CommonConstants.FOOD_IMAGE_DIR + CommonConstants.SEPARATOR + fileUploadFileName;
		Path path = Paths.get(ServletActionContext.getServletContext().getRealPath(destFile));
		
		if (FileUploadUtil.uploadFile(fileUpload, path) == true) {
			food.setImage(destFile);
		}
		foodBLO.updateFood(food);
		return SUCCESS;
	}
	
	public String delete() {
		foodBLO.deleteFood(food);
		return SUCCESS;
	}
	
	public List<Food> getListFood() {
		return listFood;
	}

	public void setListFood(List<Food> listFood) {
		this.listFood = listFood;
	}

	public Food getFood() {
		return food;
	}

	public void setFood(Food food) {
		this.food = food;
	}

	public List<FoodType> getListFoodType() {
		return listFoodType;
	}

	public void setListFoodType(List<FoodType> listFoodType) {
		this.listFoodType = listFoodType;
	}

	public File getFileUpload() {
		return fileUpload;
	}

	public void setFileUpload(File fileUpload) {
		this.fileUpload = fileUpload;
	}

	public String getFileUploadContentType() {
		return fileUploadContentType;
	}

	public void setFileUploadContentType(String fileUploadContentType) {
		this.fileUploadContentType = fileUploadContentType;
	}

	public String getFileUploadFileName() {
		return fileUploadFileName;
	}

	public void setFileUploadFileName(String fileUploadFileName) {
		this.fileUploadFileName = fileUploadFileName;
	}
}
